package org.example;

import com.alibaba.fastjson.JSON;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Config;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.index.mapper.ObjectMapper;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class meilisearch {

    private static final String ES_URL = "http://192.168.186.66:7700";

    private Client client;

    private static final int threadCount = 10;

    private static final int batchSize = 10000;

    private static final AtomicInteger counter = new AtomicInteger(0);

    @Before
    public void before() {
        client = new Client(new Config(ES_URL));
    }

    @Test
    public void createIndex() throws Exception{
        client.createIndex("ep_tag", "tag_id");
    }

    @Test
    public void deleteIndex() throws Exception {
        client.deleteIndex("ep_tag");
    }

    @Test
    public void testIndex() throws Exception {
        Map<String, Object> map = new HashMap<>();
        int tagId = counter.getAndIncrement();
        map.put("tag_id", tagId);
        map.put("tag_name", "123sq178");
        map.put("tag_value", "Amelie Poulain");

        client.index("ep_tag").addDocuments(JSON.toJSONString(map));
    }

    @Test
    public void insertData() throws Exception {
        // list转json
        // 每个线程1万，
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
        CountDownLatch latch = new CountDownLatch(threadCount);

        StopWatch stopWatch = new StopWatch();
        stopWatch.start("totalTimes");

        for (int i = 0; i < threadCount; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        StopWatch stopWatch = new StopWatch();
                        stopWatch.start(Thread.currentThread().getName());

                        List<Map<String, Object>> list = new ArrayList<>();
                        for (int i = 0; i < batchSize; i++) {
                            Map<String, Object> map = new HashMap<>();
                            int tagId = counter.getAndIncrement();
                            map.put("tag_id", tagId);
                            map.put("tag_name", "name" + tagId);
                            map.put("tag_value", "value" + tagId);
                            list.add(map);
                        }
                        client.index("ep_tag").addDocuments(JSON.toJSONString(list));
                        stopWatch.stop();
                        log.info("result = {}, {}条, time = {}", true, batchSize, stopWatch.shortSummary());
                        latch.countDown();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        latch.await();
        stopWatch.stop();
        log.info("{}条，耗时：{}", threadCount * batchSize, stopWatch.shortSummary());

        Thread.sleep(Integer.MAX_VALUE);

        client.index("ep_tag").addDocuments("[{"
                + "\"id\": 287947,"
                + "\"title\": \"Shazam\","
                + "\"poster\": \"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\","
                + "\"overview\": \"A boy is given the ability to become an adult superhero in times of need with a single magic word.\","
                + "\"release_date\": \"2019-03-23\""
                + "}]"
        );
    }
}
